﻿using System;
using System.Drawing;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using FxCore.Base;

namespace FxCore.MapSrc
{
    public abstract class AbstractMapSrc : IFxMapSrc
    {
        protected IFxProjection Projection;

        protected virtual FxTile RequestTile(string url)
        {
            HttpWebRequest request = WebRequest.CreateHttp(url);
            try
            {
                HttpWebResponse response = (HttpWebResponse) request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                {
                    if (responseStream == null)
                    {
                        throw new NullReferenceException();
                    }
                    Image image = Image.FromStream(responseStream);
                    image.Save("tmp.png");
                }
            }
            catch (WebException e)
            {
                Debug.WriteLine($"{url}=>{e.Message}");
                return FxTile.Empty;
            }
            
            return FxTile.Empty;
        }

        public string GetProjection()
        {
            return this.Projection.ToString();
        }
    }
}
